ol_drag.js ➔ handleDragEvent   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 16
rs 9.8
cc 1
1
/* 
2
 * this code based on https://openlayers.org/en/latest/examples/custom-interactions.html
3
 */
4
5
/** global: ol */
6
/** global: tmpLayer */
7
8
window.app = {};
9
var app = window.app;
10
app.Drag = function () {
11
    ol.interaction.Pointer.call(this, {
12
        handleDownEvent: handleDownEvent,
13
        handleDragEvent: handleDragEvent,
14
        handleMoveEvent: handleMoveEvent,
15
        handleUpEvent: handleUpEvent
16
    });
17
18
    this.coordinate_ = null;
19
    this.cursor_ = 'pointer';
20
    this.feature_ = null;
21
    this.previousCursor_ = undefined;
22
};
23
24
ol.inherits(app.Drag, ol.interaction.Pointer);
25
26
function handleDownEvent(evt) {
27
    var map = evt.map;
28
    var feature = map.forEachFeatureAtPixel(evt.pixel,
29
            basicFeatureHandler,
30
            {layerFilter: layerFilter}
31
    );
32
    if (feature) {
33
        this.coordinate_ = evt.coordinate;
34
        this.feature_ = feature;
35
    }
36
    return !!feature;
37
}
38
39
function handleDragEvent(evt) {
40
    var map = evt.map;
41
    var feature = map.forEachFeatureAtPixel(evt.pixel,
0 ignored issues
show
Unused Code introduced by
The variable feature seems to be never used. Consider removing it.
Loading history...
42
            basicFeatureHandler,
43
            {layerFilter: layerFilter});
44
45
    var deltaX = evt.coordinate[0] - this.coordinate_[0];
46
    var deltaY = evt.coordinate[1] - this.coordinate_[1];
47
48
    var geometry = /** @type {ol.geom.SimpleGeometry} */
49
            (this.feature_.getGeometry());
50
    geometry.translate(deltaX, deltaY);
51
52
    this.coordinate_[0] = evt.coordinate[0];
53
    this.coordinate_[1] = evt.coordinate[1];
54
}
55
56
function handleMoveEvent(evt) {
57
    if (this.cursor_) {
58
        var map = evt.map;
59
        var feature = map.forEachFeatureAtPixel(evt.pixel,
60
                basicFeatureHandler,
61
                {layerFilter: layerFilter});
62
        var element = evt.map.getTargetElement();
63
        if (feature) {
64
            if (element.style.cursor != this.cursor_) {
65
                this.previousCursor_ = element.style.cursor;
66
                element.style.cursor = this.cursor_;
67
            }
68
        } else if (this.previousCursor_ !== undefined) {
69
            element.style.cursor = this.previousCursor_;
70
            this.previousCursor_ = undefined;
71
        }
72
    }
73
}
74
75
function handleUpEvent(evt) {
76
    setTmpPointer(evt.coordinate);
77
    this.coordinate_ = null;
78
    this.feature_ = null;
79
    return false;
80
}
81
82
function layerFilter(layer) {
83
    if (layer === tmpLayer) {
84
        return(true);
85
    }
86
    return(false);
87
}
88
89
function basicFeatureHandler(feature, layer) {
0 ignored issues
show
Unused Code introduced by
The parameter layer is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
90
    return(feature);
91
}
92
93
94